package StudyCode.summary.aclfile;

import java.io.*;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class SaveReadVariousDataTypeValue {
	static final String dataFile = "invoicedata";

	static final double[] prices = {19.99, 9.99, 15.99, 3.99, 4.99};
	static final int[] units = {12, 8, 13, 29, 50};
	static final String[] descs = {"Java tah1986.com", "Java www.tah1986.com",
			"www.tah1986.com", "tah1986.com", "tah1986"};

	public static void main(String[] args) throws IOException {

		DataOutputStream out = null;

		try {
			out = new DataOutputStream(new BufferedOutputStream(
					new FileOutputStream(dataFile)));

			for (int i = 0; i < prices.length; i++) {
				out.writeDouble(prices[i]);
				out.writeInt(units[i]);
				out.writeUTF(descs[i]);
			}
		} finally {
			out.close();
		}

		DataInputStream in = null;
		double total = 0.0;
		try {
			in = new DataInputStream(new BufferedInputStream(
					new FileInputStream(dataFile)));

			double price;
			int unit;
			String desc;

			try {
				while (true) {
					price = in.readDouble();
					unit = in.readInt();
					desc = in.readUTF();
					System.out.format("订购了 %d units of %s at $%.2f%n", unit,
							desc, price);
					total += unit * price;
				}
			} catch (EOFException e) {
			}
			System.out.format("总计: $%.2f%n", total);
		} finally {
			in.close();
		}
	}
}
